
// This class handles the incoming chat stuff..
// The class contains the array with all the instances of the dialogs,
// and should be used when something is triggered related to the conversations,
// messages, etc.

var ChatboxChat = new Class({
	
	// Set the property convs to an array.
	initialize: function()
	{
		this.convs = new Array();
		
	}, // end function initialize
	
	// Focus a chat dialog
	focus: function(c)
	{
		// First check if the conversation even exists!
		if (this.exists(c))
		{
			// Show the panel
			chat.getClsDialog().show(this.convs[c]['panel']);
			
		} // end if
		
	}, // end function focus
	
	// This function looks for a conversation, if it exists.
	// Returns true when it exists.
	exists: function(c)
	{
		if (this.convs[c] != null)
			return true;
		else
			return false;
			
	},
	
	// Create a new chat dialog
	create: function(c, p)
	{
		// First check if this conversation is already added.
		// If so, we can't create a new one can we?
		if (this.exists(c))
			return;
		
		// Add the conversation to the conversation array
		this.convs[c] = {
			panel: new DlgChatboxCHAT(chat, c),
			participants: p,
			id: c
		};
		
		// Create the dialog and display it.
		chat.getClsDialog().create(this.convs[c]['panel']);
		
	}, // end function create
	
	// This function generates a title for a new chat window.
	getTitle: function(c)
	{
		// See if the conversation even exists..
		if (this.exists(c) == false)
			return 'Invalid chat.';
		
		// Get the conversation
		var chatO = this.convs[c];
		
		// When there is more than 2 participants, we can't be more accurate than just say
		// it's a group or channel chat
		if (chatO['participants'].length > 2)
			return 'Group chat';
		
		// When we have a one-on-one conversation , we can be accurate!
		if (chatO['participants'].length == 2)
		{
			// Specify the other user
			var other;
			
			for (var i = 0; i < chatO['participants'].length; i++)
			{
				if (chatO['participants'][i] != chat.getClsSession().getID())
				{
					other = chatO['participants'][i];
					
					break;
					
				} // end if
				
			} // end for
			
			// Retrieve the nickname of the other user.
			return 'Chat with ' + chat.getChat().getClsContacts().getNickname(other);
			
		} // end if
		
	}, // end function getTitle
	
	// This function closes ALL the conversations.
	// It should only be used when logging out.
	reset: function()
	{
		for (var i = 0; i < this.convs; i++)
		{
			this.convs[i]['panel'].updateContents('CLEARED');
			
			chat.getClsDialog().close(this.convs[i]['panel']);
			
		} // end for
		
	} // end function close
	
});

// Start a new chat conversation!
// int id: The other user (type=s) or conversation ID (type=c)
// char type:
//  s = single (one-to-one conversation)
//  c = conversation (group, channel)

chat_start = function(id, type)
{
	// Do something different for all types.
	switch (type)
	{
		// One-to-one chat.
		case 's':
			
			// Is this person online? If not.. we can't start a new chat.
			if (chat.getChat().getClsContacts().isOnline(id) == false)
				return false;
			
			// Request the new conversation by sending a request to the server.
			chat.getClsAjaxReq().request('chat.conv.new', ['p', 'c'], [id, 0], function(t)
			{
				// We got something back. Let's see what we got.
				var lines = t.split("\n");
				
				// Oh dear, we got nothing on the first line.. Must be an error so exit.
				if (lines[0] == '')
					return;
				
				// The first line has 2 possibilities:
				// 1: The chat conversation already exists and the user has it opened.
				// 0: The chat conversation does not exists, or the user has it closed.
				
				// The second line is the conversation ID.
				// The other lines differs: When the conversation already exists, it contains
				// nothing. If it doesn't exists (or the user has it closed), then it contains
				// all the persons which are in the conversation.
				
				// We have it open, so focus the chat window.
				if (lines[0].toInt() == 1)
					chat.getChat().getClsChat().focus(lines[1]);
				
				// The conversation is not open yet!
				if (lines[0].toInt() == 0)
				{
					// Get all the persons out of the response
					var p = new Array();
					
					for (var i = 2; i < lines.length; i++)
						p[p.length] = lines[i].toInt();
					
					// Create the new chat window.
					chat.getChat().getClsChat().create(lines[1], p);
					
				} // end if
				
			}, 'Loading conversation...', 2);
			
		break;
		
		// TODO: Make support for group conversations.
		
	} // end switch
	
};

// Before we say this library is loaded, we need the panel chat.
// This panel will contain group and single conversations.
cimport('panels.Chatbox.Chat', function()
{
	// Yay, all loaded. This library is done
	cimport_loaded('chatbox.Chat');
	
}, null);